package com.hsd.znsh.filter;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.hsd.znsh.code.ValidateCodeException;
import com.hsd.znsh.entity.User;
import com.hsd.znsh.enums.ResultEnum;
import com.hsd.znsh.properties.SecurityConstants;
import com.hsd.znsh.service.RedisService;
import com.hsd.znsh.utils.ResultVOUtil;
import org.springframework.core.annotation.Order;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;
import org.springframework.web.filter.OncePerRequestFilter;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/*
 * @auther: wangjiacai
 *
 * @create: 2018-04-28 15:46:47
 */
@WebFilter(value = "/app/*")
public class TokenFilter implements Filter {

    private RedisService redisService;

    private ObjectMapper objectMapper;

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        ServletContext servletContext=filterConfig.getServletContext();
        WebApplicationContext context= WebApplicationContextUtils.getWebApplicationContext(servletContext);
        this.redisService=context.getBean(RedisService.class);
        this.objectMapper=context.getBean(ObjectMapper.class);
    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        HttpServletRequest request = (HttpServletRequest) servletRequest;
        HttpServletResponse response = (HttpServletResponse) servletResponse;
        if(request.getRequestURI().equals(request.getContextPath()+"/app/user/login")){
            filterChain.doFilter(request,response);
            return;
        }
        if(request.getRequestURI().equals(request.getContextPath()+"/app/user/sendSmsCode")){
            filterChain.doFilter(request,response);
            return;
        }
        if(request.getRequestURI().equals(request.getContextPath()+"/app/user/register")){
            filterChain.doFilter(request,response);
            return;
        }
        if(request.getRequestURI().equals(request.getContextPath()+"/app/getFarmImage")){
            filterChain.doFilter(request,response);
            return;
        }
        if(request.getRequestURI().equals(request.getContextPath()+"/app/getSensorImage")){
            filterChain.doFilter(request,response);
            return;
        }
        if(request.getRequestURI().equals(request.getContextPath()+"/app/gateway/gatewayInvalid")){
            filterChain.doFilter(request,response);
            return;
        }
        String token = request.getHeader("token");
        if(token==null||redisService.get(token)==null){
            response.setContentType("application/json;charset=UTF-8");
            response.getWriter().write(objectMapper.writeValueAsString(ResultVOUtil.error(ResultEnum.ILLEGAL_USERS)));
        }else{
            filterChain.doFilter(request,response);
            redisService.set(token,redisService.get(token, User.class));
        }
    }

    @Override
    public void destroy() {

    }
}
